package com.leff.cards.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.leff.cards.R;
import com.leff.cards.ui.util.HistoryListAdapter;

public class HistoryFragment extends SherlockFragment {
	
	private HistoryListAdapter mAdapter;
	private TextView mNoHistoryText;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mAdapter = new HistoryListAdapter(getActivity());
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if(container == null) {
			return null;
		}
		View V = inflater.inflate(R.layout.history_fragment, null);
		
		final ExpandableListView list = (ExpandableListView)V.findViewById(R.id.history_list);
		mNoHistoryText = (TextView)V.findViewById(R.id.no_history_text);
		
		list.setAdapter(mAdapter);
		list.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int group) {
				int groupCount = mAdapter.getGroupCount();
				for(int i = 0; i < groupCount; i++) {
					if(i != group) {
						list.collapseGroup(i);
					}
				}
			}
		});
		if(mAdapter.getGroupCount() > 0) {
			list.expandGroup(0);
		}
		
		return V;
	}

	@Override
	public void onResume() {
		super.onResume();
		
		checkNoHistoryText();
	}
	
	private void checkNoHistoryText() {
		if(mAdapter.getGroupCount() < 1) {
			mNoHistoryText.setVisibility(View.VISIBLE);
		} else {
			mNoHistoryText.setVisibility(View.GONE);
		}
	}
}
